package com.sx.manage.servlet;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.sx.cache.Cache;
import com.sx.cache.Session;
import com.sx.common.util.MsgCodeUtil;
import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.listener.MCacheListener;
import com.sx.manage.util.FormPageUtil;
import com.sx.manage.util.SessionManager;
import com.sx.model.constants.MBooleanType;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.manage.mybatis.form.BaseForm;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;



/**
 * 继承 spring 类
 * @author lenovo
 *
 */
public class ViewAndModel extends ModelAndView {

	private Cache cache;
	private HttpServletRequest req;
	
	public HttpServletRequest getReq() {
		return req;
	}

	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

	public Cache getCache() {
		return cache;
	}

	public void setCache(Cache cache) {
		this.cache = cache;
	}

	public ViewAndModel(HttpServletRequest req,Cache cache) {
		super();
		this.setCache(cache);
		this.setReq(req);
		//记载缓存
		loadProperties();
		//获取用户
		consumer();
	}

	public ViewAndModel(String viewName,HttpServletRequest req,Cache cache) {
		super();
		this.setCache(cache);
		this.setReq(req);
		this.setViewName(viewName);
		//记载缓存
		loadProperties();
		//获取用户
		consumer();
	}
	/**
	 * 加载缓存信息
	 * @param req
	 * @param cache
	 */
	private void loadProperties(){
		Map<String, String> properMap = MCacheListener.getPeoperties();
		Iterator<String> its = properMap.keySet().iterator();
		String itKey = null;
		while(its.hasNext()){
			itKey = (String)its.next();
			this.addObject( itKey , properMap.get(itKey)  );
		}
	}
	/**
	 * 获取登录用户信息
	 * @param req
	 * @param cache
	 */
	private void consumer(){
		Session session = SessionManager.instance(req, null, cache);
		// 判断当前的用户是否登陆
				if (!session.istokenexists()) {
					this.setViewName("entry/m_entry");
					this.addObject("error", MsgCodeUtil.getMsg("sj.m.login.over.time.or.not.login") );					
				} else {					
					 String userJson  =  session.get("user");
					   if( StringUtils.isBlank(userJson)){
						   this.setViewName("entry/m_entry");
						   this.addObject("error", MsgCodeUtil.getMsg("sj.m.login.over.time.or.not.login") );
					   }else{
						   //获取 护具
						   session.expire(MSystemConstants.SESSION_TIME_SET_UP);
						   MConsumerLoginVO consumerLoginVO = JackJsonUtil.jsonToJavaBean(userJson, MConsumerLoginVO.class);
						   this.addObject("user", consumerLoginVO );
						   this.addObject("func_list", MSystemConstants.function_cache_query_function_vo(MSystemConstants.CACHE_TYPE_SYSTEM_CONSUMER_FUNCTION,  consumerLoginVO.getConsumer_id(),null , cache ));
							
					   }
				     
				}
	}
	/**
	 * 解决token超时问题（超时后点击菜单界面会报错但不会跳转到登录页）
	 */
	public void setViewName(String viewName) {
		if( StringUtils.isBlank( this.getViewName() )){
			super.setViewName(viewName);
		}
	}
	
	public void pageInit(BaseForm form,Class<?> cls, String pageConstants, MConsumerLoginVO loginVO) {
		if(MBooleanType.YES.equals(form.getPageClear())) {
			FormPageUtil.clearPage(cache,pageConstants, loginVO);
		}else {
			this.addObject("pageForm", FormPageUtil.getPage(cache,cls, pageConstants, loginVO));
		}
	}
}
